package com.learner.exam.controller;

import com.learner.common.util.JwtUtils;
import com.learner.exam.service.ExamRecordService;
import com.learner.pojo.exam.ExamRecord;
import com.learner.pojo.exam.vo.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author linweibing
 * @date 2023-03-10 23:41
 * @description: TODO
 */
@RestController
@RequestMapping("/api/exam/record")
public class ExamRecordController {

    @Autowired
    ExamRecordService examRecordService;


    /**
     * 获取个人成绩(分页 根据考试名查询)
     *
     * @param pageNo   page
     * @param pageSize pageSize
     * @param examId   考试id
     * @return 结果
     */
    @GetMapping("/myGrade")
    public PageResponse<ExamRecord> getMyGrade(Integer pageNo, Integer pageSize, @RequestParam(required = false) Integer examId, HttpServletRequest request) {
        String userId = JwtUtils.getCurrentUserId(request);
        return examRecordService.getUserGrade(userId, examId, pageNo, pageSize);
    }


    /**
     * 生成证书接口
     *
     * @param examName     考试名
     * @param examRecordId 考试记录id
     * @param response     response
     */
    @GetMapping("/certificate")
    public void getCertificate(HttpServletResponse response, @RequestParam(name = "examName") String examName, @RequestParam(name = "examRecordId") Integer examRecordId) throws UnsupportedEncodingException {
        examRecordService.createExamCertificate(response, URLDecoder.decode(examName, "UTF-8"), examRecordId);
    }


    /**
     * 添加考试记录
     *
     * @param examRecord 考试记录
     * @param request    request
     * @return 结果
     */
    @PostMapping("/add")
    public Integer addExamRecord(@RequestBody ExamRecord examRecord, HttpServletRequest request) {
        return examRecordService.addExamRecord(examRecord, request);
    }


    /**
     * 根据id获取考试记录
     *
     * @param recordId 记录id
     * @return ExamRecord
     */
    @GetMapping("/{recordId}")
    public ExamRecord getExamRecordById(@PathVariable("recordId") Integer recordId) {
        return examRecordService.getExamRecordById(recordId);
    }

    /**
     * 获取考试记录信息,(pageNo,pageSize)
     *
     * @param examId   考试id
     * @param page     页码
     * @param pageSize 页大小
     * @return 结果
     */
    @GetMapping("/getExamRecord")
    public PageResponse<ExamRecord> getExamRecord(@RequestParam(required = false) Integer examId, Integer page, Integer pageSize) {
        return examRecordService.getExamRecord(examId, page, pageSize);
    }


    /**
     * 设置考试记录的客观题得分,设置总分为逻辑得分+客观题
     *
     * @param totalScore   总成绩
     * @param examRecordId 考试记录id
     */
    @GetMapping("/setObjectQuestionScore")
    public void setObjectQuestionScore(Integer totalScore, Integer examRecordId) {
        examRecordService.setObjectQuestionScore(totalScore, examRecordId);
    }
}
